package com.hang.gymmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.common.Response;
import com.hang.gymmanager.exception.BusinessException;
import com.hang.gymmanager.req.AddActivityReq;
import com.hang.gymmanager.req.BuyActivityReq;
import com.hang.gymmanager.req.PageActivityReq;
import com.hang.gymmanager.req.UpdateActivityReq;
import com.hang.gymmanager.res.*;
import com.hang.gymmanager.service.ActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 活动控制器
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/3/20 19:28
 */
@RestController
@Slf4j
@RequestMapping("/activity")
@RequiredArgsConstructor
@CrossOrigin
public class ActivityController {

    private final ActivityService activityService;


    /**
     * 查询活动
     * @param req
     * @return
     */
    @PostMapping("/all")
    public Response<Page<PageActivityRes>> allActivity(@RequestBody PageActivityReq req){
        Page<PageActivityRes> info = activityService.allActivity(req);
        return Response.success(info);
    }

    /**
     * 获取场地使用列表
     * @return
     */
    @GetMapping("/place/record")
    public Response<List<AddActPlaceRes>> placeRecord(){
        List<AddActPlaceRes> addActPlaceRes = activityService.placeRecord();
        return Response.success(addActPlaceRes);
    }

    /**
     * 添加活动
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Response<Integer> addActivity(@RequestBody @Valid AddActivityReq req){
        if(req.getList().size()==0||req.getList()==null){
            throw new BusinessException("预定时间段不能为空");
        }
        Integer id = activityService.addActivity(req);
        return Response.success(id);
    }

    /**
     * 更新活动图片
     * @param file
     * @return
     */
    @PostMapping("/uploadPic")
    public Response<String> uploadPic(@RequestParam("file") MultipartFile file){
        String s = activityService.uploadPic(file);
        return Response.success(s);
    }

    @PostMapping("/update")
    public Response updateActivity(@RequestBody UpdateActivityReq req){
        activityService.updateActivity(req);
        return Response.success();
    }


    /**
     * 活动详情
     * @param actId
     * @return
     */
    @GetMapping("/detail/{actId}")
    public Response<ActivityDetailRes> detail(@PathVariable Integer actId){
        ActivityDetailRes res = activityService.detail(actId);
        return Response.success(res);
    }

    /**
     * 删除活动
     * @param activityId
     * @return
     */
    @GetMapping("/delete/{activityId}")
    public Response deleteActivity(@PathVariable Integer activityId){
        activityService.deleteActivity(activityId);
        return Response.success();
    }

    /**
     * 用户端获取活动列表
     * @param req
     * @return
     */
    @PostMapping("/userActivityPage")
    public Response<Page<UserPlacePageRes>> userActivityPage(@RequestBody BasePageReq req){
        Page<UserPlacePageRes> res = activityService.userActivityPage(req);
        return Response.success(res);
    }

    /**
     * 用户获取活动详情
     * @param activityId
     * @return
     */
    @GetMapping("/userActivityDetail/{activityId}")
    public Response<UserActivityDetailRes> userActivityDetail(@PathVariable Integer activityId){
        UserActivityDetailRes res = activityService.userActivityDetail(activityId);
        return Response.success(res);
    }


    /**
     * 购买活动票
     * @param req
     * @return
     */
    @PostMapping("/buy")
    public Response<String> buy(@RequestBody BuyActivityReq req){
        String message = activityService.buy(req);
        return Response.success(message);
    }






}
